<?php

/**
 *
 * @file   Menu.php
 * @date   2016-8-30 11:46:22
 * @author Zhenxun Du<5552123@qq.com>
 * @version    SVN:$Id:$
 */

namespace app\admin\controller;

use think\Db;
use think\Loader;

class TicketController extends CommonController
{

    public function index()
    {
        $res = db('menu')->order('listorder asc')->select();

        $lists = nodeTree($res);
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    public function getTicketList()
    {

        $start = input('start');
        $length = input('stalengthrt');
        $draw = input('draw');
        $search = input('post.search/a');
        $search = $search['value'];
        //$ser = $search?['','like','%'.$search.'%']:[];

        $data = Db::name("images_infos")
            ->alias('a')
            ->join('fd_user u', 'u.id=a.user_id')
            ->join('user_extend b', 'u.phone=b.phone')
            ->where('nickname', 'like', "%$search%")
            ->limit($start * $length, $length)->select();
        $count = Db::name("images_infos")
            ->alias('a')
            ->join('fd_user u', 'u.id=a.user_id')
            ->join('user_extend b', 'u.phone=b.phone')
            ->where('nickname', 'like', "%$search%")
            ->count();
        if ($count) {
            foreach ($data as &$v) {
                $v['image_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$v['image_url'];
                $v['options'] = "<div class='hidden-sm hidden-xs action-buttons'>
									<a class='green' href='/admin/user/info?id=" . $v['id'] . "'>
										<i class='ace-icon fa fa-pencil bigger-130'></i>
									</a>
								</div>";
            }
        }
        $temp = [
            'draw' => $draw,
            'data' => $data,
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
        ];
        echo json_encode($temp);
    }

    /*
     * 查看
     */

    public function info()
    {


        $id = input('id');
        if ($id) {
            //当前用户信息
            $info = db('user')->find($id);
            $this->assign('info', $info);
        }

        //下拉菜单
        //$this->assign('selectMenu', Loader::model('Menu')->selectMenu());
        return $this->fetch();
    }

    /*
     * 添加
     */

    public function add()
    {
        $data = input();
        if ($data['parentid'] == null) {
            $data['parentid'] = 0;
        }
        $res = model('menu')->allowField(true)->save($data);
        if ($res) {
            $this->success('操作成功', url('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /*
     * 修改
     */

    public function edit()
    {

        $data = input();
        $data['updated_at'] = date('Y-m-d H:i:s',time());
        $res = model('user')->allowField(true)->save($data, ['id' => $data['id']]);
        if ($res) {
            $this->success('操作成功', url('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /*
     * 删除
     */

    public function del()
    {
        $id = input('id');
        $res = db('menu')->where(['id' => $id])->delete();
        if ($res) {
            $this->success('操作成功', url('index'));
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 排序
     */
    public function setListorder()
    {

        if ($_POST['listorder']) {
            $listorder = $_POST['listorder'];
            foreach ($listorder as $k => $v) {
                $data = array();
                $data['listorder'] = $v;
                $data['updatetime'] = time();
                $res = db('menu')->where(['id' => $k])->update($data);
            }
            if ($res) {
                $this->success('操作成功', url('index'));
            } else {
                $this->error('操作失败');
            }
        }
    }

}
